clc
clear
close all
%
%%
TimeDuration = 0.1;
SampleRate = 200e3;
MasterClockRate = 600e3;
ModulatorOrder = 2;
SamplePerSymbol = 8;

ModulatorConfig.BandwidthTimeProduct = 0.3;
ModulatorConfig.PulseLength = 4;
ModulatorConfig.SymbolPrehistory = 1;
ModulatorConfig.InitialPhaseOffset = 0;

CarrierFrequency = 200e3;
IqImbalanceConfig.A = 3;
IqImbalanceConfig.P = 10;
PhaseNoiseConfig.Level = -50;
PhaseNoiseConfig.FrequencyOffset = 20;
MemoryLessNonlinearityConfig.Method = 'Saleh model';
MemoryLessNonlinearityConfig.InputScaling = 0;
MemoryLessNonlinearityConfig.AMAMParameters = [2.1587 1.1517];
MemoryLessNonlinearityConfig.AMPMParameters =  [4.0033 9.1040];
MemoryLessNonlinearityConfig.OutputScaling = 0;
MemoryLessNonlinearityConfig.ReferenceImpedance = 1;
ThermalNoiseConfig.NoiseTemperature = 290;
AGCConfig.AveragingLength = 256 * 4;
AGCConfig.MaxPowerGain = 400;

source = RandomSource(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol);

x = source();

%% 单天线场景

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
rayChannel = Rayleigh(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

ricChannel = Rician(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=1, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = GMSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    ModulatorConfig=ModulatorConfig);

x1= baseBandSignal(x);
x1 = txRF(x1);
x1 = rayChannel(x1);
y1 = rxRF({x1});

%%

gmskmodulator = comm.GMSKModulator( ...
    BitInput=true, ...
    InitialPhaseOffset=pi/4);
gmskdemodulator = comm.GMSKDemodulator( ...
    BitOutput=true, ...
    InitialPhaseOffset=pi/4);
errorRate = comm.ErrorRate( ...
    ReceiveDelay=gmskdemodulator.TracebackDepth);
errorRate1 = comm.ErrorRate( ...
    ReceiveDelay=gmskdemodulator.TracebackDepth);
data = randi([0 1],100000,1);
modSignal = gmskmodulator(data);
bw = obw(modSignal, 200e3, [], 99);
bw = ceil(bw/1000)*1000;
a = lowpass(modSignal, bw/2, 200e3, ...
    ImpulseResponse = "fir", ...
    Steepness = 0.99999, StopbandAttenuation=200);
receivedData = gmskdemodulator(modSignal);
receivedData1 = gmskdemodulator(a);
errorStats = errorRate(data, receivedData);
errorStats1 = errorRate1(data, receivedData1);